package com.payment.plugin.hangzhoubank;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.payment.plugin.PaymentPlugin;
import com.payment.request.TransRequest;
import com.payment.util.SignUtil;
import com.payment.util.WebUtils;
import com.payment.vo.PayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component("hangZhouBankAliPayPlugin")
public class HangZhouBankAliPayPlugin extends PaymentPlugin {

    @Autowired
    private SignUtil signUtil;


    @Override
    public PayResult pay(TransRequest request) {
        SimpleDateFormat dateType = new SimpleDateFormat("yyyyMMddHHmmss");
        PayResult payResult = new PayResult();
        Map<String, Object> params = getCommonParams();
        //证书ID
        params.put("certId", "17010100001");
        //交易类型
        params.put("txnType","1007");
        //交易子类型
        params.put("txnSubType","100706");
        //接入方式
        params.put("aesWay","04");
        //商户编号
        params.put("merId","8201802260000002");
        //后台通知地址
        params.put("backEndUrl","dsadsad");
        //商户订单号
        params.put("txnOrderId",request.getTransNo());
        //商户订单发送时间
        params.put("txnOrderTime",dateType.format(new Date()));
        //账户类型  03：支付宝
        params.put("txnAccType","03");
        //用户标识
        params.put("buyerId",request.getUserId().toString());
        //支付订单描述
        params.put("txnOrderBody",request.getRemark());
        //支付金额 订单总金额(交易单位为分 例:1.23 元=123)，
        params.put("txnAmt",String.valueOf(request.getMoney().multiply(BigDecimal.valueOf(100)).longValue()));
        //币种  156：人民币
        params.put("txnCcyType","156");
        //签名
        signUtil.dataSign(params,"UTF-8");
        String signAture = null;
        try {
            signAture = WebUtils.doPostJson("http://test.hccb.cc/ifsp-payweb/SOA/txnAccept/", JSONObject.toJSON(params).toString(),"UTF-8",30000
                    ,30000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        params.put("signAture",signAture);
        //接收消息
        SignUtil.dataVerify((Map) JSON.parse(signAture),"UTF-8");
        Map<String, Object> result = WebUtils.comm(params);
//        WebUtils.doPost("http://test.hccb.cc/ifsp-payweb/SOA/txnAccept/",params,5,5);
        return payResult;
    }

    /**
     * 支付结果查询
     * @param request
     * @return
     */
    @Override
    public PayResult payQuery(TransRequest request) {
       return resultQuery(request);
    }

    @Override
    public PayResult refund(TransRequest request) {
        SimpleDateFormat dateType = new SimpleDateFormat("yyyyMMddHHmmss");
        PayResult payResult = new PayResult();
        Map<String, Object> params = getCommonParams();
        //交易类型
        params.put("txnType","1008");
        //交易子类型
        params.put("txnSubType","100800");
        //接入方式
        params.put("aesWay","04");
        //商户编号
        params.put("merId","8201802260000002");
        //商户订单号
        params.put("txnOrderId",request.getTransNo());
        //商户订单发送时间
        params.put("txnOrderTime",dateType.format(new Date()));
        //原商户订单号
        params.put("origTxnOrderId",request.getTransNo());
        //原始平台交易流水号
        params.put("origRespTxnSsn",request.getTransNo());
        //原始平台受理时间
        params.put("origRespTxnTime",request.getTransNo());
        //退款金额 订单总金额(交易单位为分 例:1.23 元=123)，
        params.put("txnAmt",String.valueOf(request.getMoney().multiply(BigDecimal.valueOf(100)).longValue()));
        //币种  156：人民币
        params.put("txnCcyType","156");
        Map<String, Object> result = WebUtils.comm(params);
        return payResult;
    }

    /**
     * 退款结果查询
     * @param transRequest
     * @return
     */
    @Override
    public PayResult refundQuery(TransRequest transRequest) {
        return resultQuery(transRequest);
    }

    /**
     * 公共参数
     * @return
     */
    private Map<String, Object> getCommonParams(){
        Map<String, Object> params = new HashMap<String, Object>();
        //编码方式
        params.put("encoding", "UTF-8");
        //版本号
        params.put("sdkAppId", "201701");
        return params;
    }

    /**
     * 支付/退款结果查询
     * @param request
     * @return
     */
    private PayResult resultQuery(TransRequest request){
        SimpleDateFormat dateType = new SimpleDateFormat("yyyyMMddHHmmss");
        PayResult payResult = new PayResult();
        Map<String, Object> params = getCommonParams();
        //交易类型
        params.put("txnType","1009");
        //交易子类型
        params.put("txnSubType","100900");
        //商户编号
        params.put("merId","8201802260000002");
        //交易流水
        params.put("txnOrderId",request.getTransNo());
        //交易发送时间
        params.put("txnOrderTime",dateType.format(new Date()));
        Map<String, Object> result = WebUtils.comm(params);
        return payResult;
    }
}
